﻿using Microsoft.VisualBasic;
using NPOI.DDF;
using NPOI.SS.UserModel;
using NPOI.Util.ArrayExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ExcelSystem.ToolHelper
{
    public  static class ExtensionMethods
    {
        public static string CellToString(this ICell cell)
        {
            if (cell == null || cell.CellType == CellType.Blank)
            {
                return string.Empty;
            }
            else
            {
                return cell.ToString();
            }
        }


        public static string CellToValue(this ICell cell)
        {
            if (cell == null || cell.CellType == CellType.Blank)
            {
                return string.Empty;
            }
            else if(cell.CachedFormulaResultType== CellType.Numeric)
            {
                return cell.NumericCellValue.ToString();
            }
            else if (cell.CachedFormulaResultType == CellType.String)
            {
                return cell.StringCellValue.ToString();
            }
            else
            {
                return cell.ToString();
            }
        }

        public static string CellToNumberString(this ICell cell)
        {
            if (cell == null || cell.CellType == CellType.Blank)
            {
                return string.Empty;
            }
            else
            {
                return Regex.Replace(cell.ToString(), @"[^\d.\d]", "");
              
            }
        }

        public static string ExtractNumberString(this string str)
        {
            try
            {
                if (string.IsNullOrEmpty(str))
                {
                    return "0";
                }
                else
                {
                    Regex reg = new Regex(@"^\d+(\.\d+)?$", RegexOptions.IgnoreCase | RegexOptions.Singleline,     TimeSpan.FromSeconds(3));
                    MatchCollection col = reg.Matches(str);

                    string reuslt = "";
                    foreach (Match m in col)
                    {
                        reuslt+= m.Value;
                    }



                    return reuslt;

                }
            }
            catch { 
            return  "0";
            }
            
        }

        public static decimal? CellToDouble(this ICell cell)
        {
            if (cell == null || cell.CellType == CellType.Blank)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(cell.NumericCellValue) ;
            }
        }
        public static string[]  CellSplit(this string str,string par)
        {
            if ( string.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                return str.Split(par);
            }
        }

        /// <summary>
        /// 精确替换文字
        /// </summary>
        /// <param name="str"></param>
        /// <param name="oldval"></param>
        /// <param name="newval"></param>
        /// <returns></returns>
        public static string ReplaceNumber(this string str, string oldval, string newval)
        {

            if (string.IsNullOrEmpty(oldval))
                return str;
            //当被提取的值前后 要么是运算符 要么为空 才算精准提取
            var arr = new[] { '+', '-', '*', '/', '(', ')' };

            int start = 0;
            int index = 0;
            while (true)
            {
                if (start > str.Length)
                    break;
                //获取下标
                index = str.IndexOf(oldval, start);
                if (index == -1)
                    break;
                bool left = false;
                //判断左右两边  index-1 index+length-1
                if (index - 1 >= 0)
                {
                    var a = str[index - 1];
                    left = arr.Contains(a);
                }
                else
                {
                    left = true;
                }

                bool right = false;
                if (index + oldval.Length >= str.Length)
                {
                    right = true;

                }
                else
                {
                    var b = str[index + oldval.Length];
                    right = arr.Contains(b);
                }
                if (right && left)
                {
                    str = str.Remove(index, oldval.Length).Insert(index, newval);
                }
                start = index + oldval.Length;


            }

            return str;








        }


        /// <summary>
        /// 获取字符串重对应字符所在的位置
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<int> GetStrIndex(this string str,string par)
        {
            List<int> result=new List<int>();
            int start = 0;
            while(true)
            {
                int index = str.IndexOf(par, start);
                if(index!=-1)
                {
                    result.Add(index);
                    start = index + par.Length;
                }
                else
                {
                    break;
                }
            }
         


            return result;

        }







    }
}
